import AlgoPlus.TORAStockV3x as torastock


@torastock.flexible_callback_wraps
def flexible_callback1(event_id, field, islast, error_id, error_msg, user_id):
    print("=======my flexible callback1 decode response by wraps")
    print(event_id, islast, user_id, error_id, error_msg)
    print(field)
    print("=======")


def flexible_callback2(event_id, field, islast, user_id, error_id, error_msg):
    print("=======my flexible callback2 decode response by function")
    print(event_id, islast, user_id, error_id, error_msg)
    field = torastock.decode_event_field(event_id, field)
    print(field)
    print("=======")


if __name__ == '__main__':

    print(f"||Version||,Version:{torastock.getVersion()}")

    login_field = torastock.CAPLoginField()
    login_field.UserType = torastock.ENUM_UserType_NSIGHTStock
    login_field.UserID = ""
    login_field.Password = ""
    # NSight股票仿真交易环境（http://n-sight.com.cn）
    login_field.TraderFrontAddress = "tcp://210.14.72.11:4400"
    login_field.MdFrontAddress = "tcp://210.14.72.11:4402"

    trader = torastock.init(1, login_field, None, flexible_callback1)
    # trader = torastock.init(1, login_field, None, flexible_callback2)

    if trader is not None:
        print("||init||,Succeed")
        torastock.loop()
    else:
        error_id = torastock.getInitError()
        message = torastock.getMessageByErrorID(error_id)
        print(f"||init||,ErrorID:{error_id},Message:{message}")
